<template>
  <div class="icons">
    <swiper :options="swiperOption">
      <swiper-slide v-for="(page, index) of pages" :key="index">
        <div class="icon-img" v-for="img in page" :key="img.id">
          <img :src="img.imgUrl" alt class="icon-img-content" />
          <p>{{img.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
import iconfont from '@/assets/json/icons'
export default {
  name: 'HomeIcons',
  data() {
    return {
      swiperOption: {
        loop: true
      },
      imgList: iconfont
    }
  },
  computed: {
    // 创建页码计算
    pages() {
      const pages = []
      // 遍历接收的icons数量
      this.imgList.forEach((item, index) => {
        const page = Math.floor(index / 8)
        // 如果icons不为空则添加到 pages数组中
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    }
  }
}
</script>

<style lang="stylus" scoped>
.icons >>> .swiper-container
  width: 100%
  height: 3.75rem
  .icon-img
    float: left
    width: 25%
    height: 50%
    text-align: center
    .icon-img-content
      display: block
      width: 50%
      height: 50%
      margin: 0.2rem auto
</style>
